import random

"""
1、有一个登录系统：账号、密码、验证码，其中三个要求任意一个不满足时，返回对应的错误
如：账号错误  ，密码错误
验证码错误登录失败，都正确返回登录成功，通过函数来完成
正确用户名为user,密码user,验证码为abcd 才能通过
"""


def login_validate(uname, pas, vercode):
    if vercode != 'abcd':
        msg = '验证码错误'
    elif uname != 'user':
        msg = "用户名错误"
    elif pas != 'user':
        msg = "密码错误"
    else:
        msg = "登录成功"
    return msg


"""
2、求出1 / 1 + 1 / 3 + 1 / 5……+1 / 99的和
"""


def sum_odd():
    list1 = range(1, 100, 2)
    result = sum(list1) + len(list(list1))
    print("求出1 / 1 + 1 / 3 + 1 / 5……+1 / 99的和 = ", result)


# 3、用循环语句，计算2 - 10之间整数的循环相乘的值
def mult():
    mu = 1;
    for i in range(2, 11):
        mu *= i
    print("循环相乘的值 2-10 乘积 = ", mu)


# 4、用for循环打印九九乘法表 略
def nine_nine_mult():
    for i in range(1, 10):
        for j in range(1, 10):
            if i < j:
                break
            print(f'{i}*{j}={i * j} ', end=" ")
        print()


# 5、求每个字符串中字符出现的个数如：helloworld!!!!!!
def str_count(ss: str):
    set1 = set()
    # 先去重复,然后计算个数
    for s1 in ss:
        set1.add(s1)
    for st in set1:
        print(f'{st} 出现的次数 {ss.count(st)}')


# 6、实现把字符串str = "duoceshi" 中任意字母变为大写
def str_up(letter: str, ss: str):
    idx = ss.find(letter)
    if idx == -1:
        print("字母不存在")
    else:
        return ss.replace(letter, letter.upper())


# 删除重复元素
def del_repeat(vlist: list):
    return list(set(vlist))


# 将字符串类似："k:1|k3:2|k2:9|...|kn:m", 处理成key: value或json格式, 比如
def str_to_dict():
    dict1 = {}
    ss = "k:1|k3:2|k2:9"
    for s in ss.split("|"):
        vkey = s.split(":")
        dict1[vkey[0]] = vkey[1]
    print("dict = ", dict1)


if __name__ == '__main__':
    print("登录验证：", login_validate("user", "oooo", "abcd"))
    print("----------------------------分割线----------------------------")
    sum_odd()
    print("----------------------------分割线----------------------------")
    mult()
    print("----------------------------分割线----------------------------")
    nine_nine_mult()
    print("----------------------------分割线----------------------------")
    str_count("aaabcdde")
    print("----------------------------分割线----------------------------")
    print("任意替换 :", str_up("o", "duoceshi"))
    print("----------------------------分割线----------------------------")
    vlist = del_repeat([1, 1, 1, 23, 4, 5, 112])
    print(vlist)
    print("----------------------------分割线----------------------------")
    str_to_dict()
